<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Go by Example 中文版: 时间</title>
    <link rel=stylesheet href="site.css">
  </head>
  <script>
      onkeydown = (e) => {
          
          if (e.key == "ArrowLeft") {
              window.location.href = 'xml';
          }
          
          
          if (e.key == "ArrowRight") {
              window.location.href = 'epoch';
          }
          
      }
  </script>
  <body>

    <div class="example" id="time">
      <h2><a href="./">Go by Example 中文版</a>: 时间</h2>
      
      <table>
        
        <tr>
          <td class="docs">
            <p>Go 为时间（time）和时间段（duration）提供了大量的支持；这儿有是一些例子。</p>

          </td>
          <td class="code empty leading">
            
          
          </td>
        </tr>
        
        <tr>
          <td class="docs">
            
          </td>
          <td class="code leading">
            <a href="http://play.golang.org/p/mRyQCXStgHO"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
          <div class="highlight"><pre><span class="kn">package</span> <span class="nx">main</span>
</pre></div>

          </td>
        </tr>
        
        <tr>
          <td class="docs">
            
          </td>
          <td class="code leading">
            
          <div class="highlight"><pre><span class="kn">import</span> <span class="p">(</span>
    <span class="s">&quot;fmt&quot;</span>
    <span class="s">&quot;time&quot;</span>
<span class="p">)</span>
</pre></div>

          </td>
        </tr>
        
        <tr>
          <td class="docs">
            
          </td>
          <td class="code leading">
            
          <div class="highlight"><pre><span class="kd">func</span> <span class="nx">main</span><span class="p">()</span> <span class="p">{</span>
    <span class="nx">p</span> <span class="o">:=</span> <span class="nx">fmt</span><span class="p">.</span><span class="nx">Println</span>
</pre></div>

          </td>
        </tr>
        
        <tr>
          <td class="docs">
            <p>从获取当前时间时间开始。</p>

          </td>
          <td class="code leading">
            
          <div class="highlight"><pre>    <span class="nx">now</span> <span class="o">:=</span> <span class="nx">time</span><span class="p">.</span><span class="nx">Now</span><span class="p">()</span>
    <span class="nx">p</span><span class="p">(</span><span class="nx">now</span><span class="p">)</span>
</pre></div>

          </td>
        </tr>
        
        <tr>
          <td class="docs">
            <p>通过提供年月日等信息，你可以构建一个 <code>time</code>。
时间总是与 <code>Location</code> 有关，也就是时区。</p>

          </td>
          <td class="code leading">
            
          <div class="highlight"><pre>    <span class="nx">then</span> <span class="o">:=</span> <span class="nx">time</span><span class="p">.</span><span class="nx">Date</span><span class="p">(</span>
        <span class="mi">2009</span><span class="p">,</span> <span class="mi">11</span><span class="p">,</span> <span class="mi">17</span><span class="p">,</span> <span class="mi">20</span><span class="p">,</span> <span class="mi">34</span><span class="p">,</span> <span class="mi">58</span><span class="p">,</span> <span class="mi">651387237</span><span class="p">,</span> <span class="nx">time</span><span class="p">.</span><span class="nx">UTC</span><span class="p">)</span>
    <span class="nx">p</span><span class="p">(</span><span class="nx">then</span><span class="p">)</span>
</pre></div>

          </td>
        </tr>
        
        <tr>
          <td class="docs">
            <p>你可以提取出时间的各个组成部分。</p>

          </td>
          <td class="code leading">
            
          <div class="highlight"><pre>    <span class="nx">p</span><span class="p">(</span><span class="nx">then</span><span class="p">.</span><span class="nx">Year</span><span class="p">())</span>
    <span class="nx">p</span><span class="p">(</span><span class="nx">then</span><span class="p">.</span><span class="nx">Month</span><span class="p">())</span>
    <span class="nx">p</span><span class="p">(</span><span class="nx">then</span><span class="p">.</span><span class="nx">Day</span><span class="p">())</span>
    <span class="nx">p</span><span class="p">(</span><span class="nx">then</span><span class="p">.</span><span class="nx">Hour</span><span class="p">())</span>
    <span class="nx">p</span><span class="p">(</span><span class="nx">then</span><span class="p">.</span><span class="nx">Minute</span><span class="p">())</span>
    <span class="nx">p</span><span class="p">(</span><span class="nx">then</span><span class="p">.</span><span class="nx">Second</span><span class="p">())</span>
    <span class="nx">p</span><span class="p">(</span><span class="nx">then</span><span class="p">.</span><span class="nx">Nanosecond</span><span class="p">())</span>
    <span class="nx">p</span><span class="p">(</span><span class="nx">then</span><span class="p">.</span><span class="nx">Location</span><span class="p">())</span>
</pre></div>

          </td>
        </tr>
        
        <tr>
          <td class="docs">
            <p>支持通过 <code>Weekday</code> 输出星期一到星期日。</p>

          </td>
          <td class="code leading">
            
          <div class="highlight"><pre>    <span class="nx">p</span><span class="p">(</span><span class="nx">then</span><span class="p">.</span><span class="nx">Weekday</span><span class="p">())</span>
</pre></div>

          </td>
        </tr>
        
        <tr>
          <td class="docs">
            <p>这些方法用来比较两个时间，分别测试一下是否为之前、之后或者是同一时刻，精确到秒。</p>

          </td>
          <td class="code leading">
            
          <div class="highlight"><pre>    <span class="nx">p</span><span class="p">(</span><span class="nx">then</span><span class="p">.</span><span class="nx">Before</span><span class="p">(</span><span class="nx">now</span><span class="p">))</span>
    <span class="nx">p</span><span class="p">(</span><span class="nx">then</span><span class="p">.</span><span class="nx">After</span><span class="p">(</span><span class="nx">now</span><span class="p">))</span>
    <span class="nx">p</span><span class="p">(</span><span class="nx">then</span><span class="p">.</span><span class="nx">Equal</span><span class="p">(</span><span class="nx">now</span><span class="p">))</span>
</pre></div>

          </td>
        </tr>
        
        <tr>
          <td class="docs">
            <p>方法 <code>Sub</code> 返回一个 <code>Duration</code> 来表示两个时间点的间隔时间。</p>

          </td>
          <td class="code leading">
            
          <div class="highlight"><pre>    <span class="nx">diff</span> <span class="o">:=</span> <span class="nx">now</span><span class="p">.</span><span class="nx">Sub</span><span class="p">(</span><span class="nx">then</span><span class="p">)</span>
    <span class="nx">p</span><span class="p">(</span><span class="nx">diff</span><span class="p">)</span>
</pre></div>

          </td>
        </tr>
        
        <tr>
          <td class="docs">
            <p>我们可以用各种单位来表示时间段的长度。</p>

          </td>
          <td class="code leading">
            
          <div class="highlight"><pre>    <span class="nx">p</span><span class="p">(</span><span class="nx">diff</span><span class="p">.</span><span class="nx">Hours</span><span class="p">())</span>
    <span class="nx">p</span><span class="p">(</span><span class="nx">diff</span><span class="p">.</span><span class="nx">Minutes</span><span class="p">())</span>
    <span class="nx">p</span><span class="p">(</span><span class="nx">diff</span><span class="p">.</span><span class="nx">Seconds</span><span class="p">())</span>
    <span class="nx">p</span><span class="p">(</span><span class="nx">diff</span><span class="p">.</span><span class="nx">Nanoseconds</span><span class="p">())</span>
</pre></div>

          </td>
        </tr>
        
        <tr>
          <td class="docs">
            <p>你可以使用 <code>Add</code> 将时间后移一个时间段，或者使用一个 <code>-</code> 来将时间前移一个时间段。</p>

          </td>
          <td class="code">
            
          <div class="highlight"><pre>    <span class="nx">p</span><span class="p">(</span><span class="nx">then</span><span class="p">.</span><span class="nx">Add</span><span class="p">(</span><span class="nx">diff</span><span class="p">))</span>
    <span class="nx">p</span><span class="p">(</span><span class="nx">then</span><span class="p">.</span><span class="nx">Add</span><span class="p">(</span><span class="o">-</span><span class="nx">diff</span><span class="p">))</span>
<span class="p">}</span>
</pre></div>

          </td>
        </tr>
        
      </table>
      
      <table>
        
        <tr>
          <td class="docs">
            
          </td>
          <td class="code leading">
            
          <div class="highlight"><pre><span class="gp">$</span> go run time.go
<span class="go">2012-10-31 15:50:13.793654 +0000 UTC</span>
<span class="go">2009-11-17 20:34:58.651387237 +0000 UTC</span>
<span class="go">2009</span>
<span class="go">November</span>
<span class="go">17</span>
<span class="go">20</span>
<span class="go">34</span>
<span class="go">58</span>
<span class="go">651387237</span>
<span class="go">UTC</span>
<span class="go">Tuesday</span>
<span class="go">true</span>
<span class="go">false</span>
<span class="go">false</span>
<span class="go">25891h15m15.142266763s</span>
<span class="go">25891.25420618521</span>
<span class="go">1.5534752523711128e+06</span>
<span class="go">9.320851514226677e+07</span>
<span class="go">93208515142266763</span>
<span class="go">2012-10-31 15:50:13.793654 +0000 UTC</span>
<span class="go">2006-12-05 01:19:43.509120474 +0000 UTC</span>
</pre></div>

          </td>
        </tr>
        
        <tr>
          <td class="docs">
            <p>接下来，我们将研究与 Unix 纪元相关的概念。</p>

          </td>
          <td class="code empty">
            
          
          </td>
        </tr>
        
      </table>
      
      
      <p class="next">
        下一个例子: <a href="epoch">时间戳</a>
      </p>
      
      <p class="footer">
        <a href="https://twitter.com/mmcgrana">@mmcgrana</a> 编写 | <a href="https://github.com/gobyexample-cn">gobyexample-cn</a> 翻译 | <a href="https://github.com/gobyexample-cn/gobyexample/issues">反馈</a> | <a href="https://github.com/gobyexample-cn/gobyexample">源码</a> | <a href="https://github.com/mmcgrana/gobyexample#license">license</a>      </p>
      </p>
    </div>
    <script>
      var codeLines = [];
      codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A    \"fmt\"\u000A    \"time\"\u000A)\u000A');codeLines.push('func main() {\u000A    p := fmt.Println\u000A');codeLines.push('    now := time.Now()\u000A    p(now)\u000A');codeLines.push('    then := time.Date(\u000A        2009, 11, 17, 20, 34, 58, 651387237, time.UTC)\u000A    p(then)\u000A');codeLines.push('    p(then.Year())\u000A    p(then.Month())\u000A    p(then.Day())\u000A    p(then.Hour())\u000A    p(then.Minute())\u000A    p(then.Second())\u000A    p(then.Nanosecond())\u000A    p(then.Location())\u000A');codeLines.push('    p(then.Weekday())\u000A');codeLines.push('    p(then.Before(now))\u000A    p(then.After(now))\u000A    p(then.Equal(now))\u000A');codeLines.push('    diff := now.Sub(then)\u000A    p(diff)\u000A');codeLines.push('    p(diff.Hours())\u000A    p(diff.Minutes())\u000A    p(diff.Seconds())\u000A    p(diff.Nanoseconds())\u000A');codeLines.push('    p(then.Add(diff))\u000A    p(then.Add(-diff))\u000A}\u000A');codeLines.push('');codeLines.push('');
    </script>
    <script src="site.js" async></script>
  </body>
</html>
